package com.zhuojy.equity.mall.admin.service.job;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhuojy.equity.mall.admin.common.entity.po.salary.SalaryWhoPO;
import com.zhuojy.equity.mall.admin.common.entity.po.salary.TUserArchivesPO;
import com.zhuojy.equity.mall.admin.common.mail.Mail;
import com.zhuojy.equity.mall.admin.common.utils.MailUtil;
import com.zhuojy.equity.mall.admin.service.salary.SalaryWhoService;
import com.zhuojy.equity.mall.admin.service.salary.TUserArchivesService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @description: TODO
 * @author chen
 * @date 2024/4/15 19:30
 * @version 1.0
 */
@Slf4j
@RequiredArgsConstructor
public class QuartzJob implements Job {
    private final TUserArchivesService tUserArchivesService;
    private final SalaryWhoService salaryWhoService;
    @Resource
    private MailUtil mailUtil;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) {

        TUserArchivesPO tUserArchivesPO = tUserArchivesService.getOne(Wrappers.lambdaQuery(TUserArchivesPO.class).eq(TUserArchivesPO::getUserId, jobExecutionContext.getJobDetail().getJobDataMap().get("userId").toString()));
        System.out.println(tUserArchivesPO);

        String recipient = tUserArchivesPO.getEmail();
        Mail mail = new Mail();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        mail.setRecipient(recipient);
        mail.setSubject("阿里巴巴网络技术有限公司 \n"+"\n"+"victory won't come to me unless I go to it");
        mail.setContent("亲爱的用户"+tUserArchivesPO.getName()+"：您好！\n" +
                "\n" + "    您上月薪资已经发放,请注意查收, 如有疑问, 请您尽快联络公司财务。\n" +
                "\n" +
                "   您的工资条如下\n" + "\n  " +
               "基本工资:" + tUserArchivesPO.getWages() +"\n" + "\n  " +
                "加班工资:" + "0"+"\n" + "\n  " +
                "请假扣款:" +"0"+"\n" + "\n  " +
                "奖金:" +"1000"+"\n" + "\n  " +
                "总工资为:"+(tUserArchivesPO.getWages()+1000)+"\n" + "\n "+
                "\n" + "\n "+
                "毅曙瑞阳项目组"+"\n" + "\n "+
                formatter.format(new Date())

        );
        mailUtil.sendSimpleMail(mail);
        salaryWhoService.update(Wrappers.lambdaUpdate(SalaryWhoPO.class).eq(SalaryWhoPO::getSalaryId,jobExecutionContext.getJobDetail().getJobDataMap().get("salaryId").toString()).eq(SalaryWhoPO::getUserId,jobExecutionContext.getJobDetail().getJobDataMap().get("userId").toString()).set(SalaryWhoPO::getIssuanceStatus,4).set(SalaryWhoPO::getConfirmTheStatus,1));
    }
}
